﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Doan_ver1
{
    public partial class WebForm7 : System.Web.UI.Page
    {
        public string connection()
        {
            return Provider.connectionString();
        }
       

      
        public string generateRandomString()
        {
            string randomString = "";
            int randNumber;
            Random rnd = new Random();

            for (int i = 0; i < 6; i++)
            {
                if (rnd.Next(1, 3) == 1)
                    randNumber = rnd.Next(97, 103); //char {a-z}
                else
                    randNumber = rnd.Next(48, 58); //int {0-9}

                randomString = randomString + (char)randNumber;
            }
            return randomString;
        }
        public void load()
        {
            SQLNganHangDataContext data = new SQLNganHangDataContext(connection());
            TaiKhoan tk = data.TaiKhoans.Where(c => c.TenDangNhap == Session["khachhang"] as string).FirstOrDefault();
            Label1.Text = tk.SoDuTK.ToString().Replace(".0000", string.Empty);
            Label2.Text = generateRandomString();
            Session["ruttien"] = Label2.Text;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["khachhang"] == null)
                Response.Redirect("DangNhap.aspx");
            if (!IsPostBack)
                load();
            
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            load();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SQLNganHangDataContext data = new SQLNganHangDataContext(connection());
            TaiKhoan tk = data.TaiKhoans.Where(c => c.TenDangNhap == Session["khachhang"] as string).FirstOrDefault();
            bool dieuKien = true;
            if (!Page.IsValid)
                dieuKien = false;
            if (Convert.ToDecimal(TextBox1.Text) > tk.SoDuTK)
                dieuKien = false;
            if (TextBox2.Text != Session["ruttien"] as string)
                dieuKien = false;
            if (dieuKien)
            {
                tk.SoDuTK -= Convert.ToDecimal(TextBox1.Text);
                data.SubmitChanges();

                ChiTietGD_NH chitiet = new ChiTietGD_NH();
                chitiet.LoaiGD_NH = data.LoaiGD_NHs.Where(c => c.MaLoaiGD_NH == "2").First();
                chitiet.MaTaiKhoan = tk.MaTaiKhoan;
                chitiet.SoTien = Convert.ToDecimal(TextBox1.Text);
                chitiet.Ngay = DateTime.Now;
                data.ChiTietGD_NHs.InsertOnSubmit(chitiet);
                data.SubmitChanges();

                LichSuGD_NH lichSu = new LichSuGD_NH();
                lichSu.MaChiTietGD_NH = chitiet.MaChiTietGD_NH;
                lichSu.MaTaiKhoan = tk.MaTaiKhoan;
                data.LichSuGD_NHs.InsertOnSubmit(lichSu);
                data.SubmitChanges();

                Label3.Text = "Rút tiền thành công";
                load();
                TextBox1.Text = null;
                TextBox2.Text = null;
            }
            else
            {
                Label3.Text = "Thất bại";
                load();
            }
        }
    }
}